// pages/myPage/myPage.js
import {moneyRecord, authInfo, withdraw} from '../../apis/user'
import util from '../../utils/util'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    showDialog: false,
    userInfo: {},
    authInfo: {},
    curMoney: 0,
    allMoney: 0,
    list: [],
    pageNum: 1,
    pageSize: 5,
    count: 0,
    pages: 0,
    amount: '',
    beginTime: util.formatTime2(util.getBeginTime()).split(" ")[0],
    endTime: util.formatTime2(new Date()).split(" ")[0],
    currentDate: new Date(),
    minDate: util.getBeginTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    }
  },

  amountOnChange(event) {
    this.setData({
      amount: event.detail,
    });
  },

  async onDialogConfirm() {
    if (this.data.amount == '' || this.data.amount == undefined) {
      wx.showToast({
        title: '请填写提现金额',
      })
    } else {
      const res = await withdraw(util.yuanToFen(this.data.amount));
      if (res.code == '200') {
        wx.showToast({
          title: '提现成功'
        })
        wx.redirectTo({
          url: '/pages/wallet/wallet',
        })
      } else {
        wx.showToast({
          title: res.message,
        })
      } 
    }
    this.setData({
      amount: ''
    })
  },

  onDialogCancel() {
    this.setData({
      amount: '',
    })
  },

  choose(e) {
    const { type } = e.currentTarget.dataset
    this.setData({
      show: true,
      type: type,
    })
    if (type == 1) {
      this.setData({ currentDate: new Date().getTime() })
    } else this.setData({ currentDate: new Date().getTime() })
  },
  onInput(event) {
    const { end, begin, type } = this.data
    if (type == 1) {
      if (event.detail >= new Date(end).getTime()) {
        wx.showToast({
          title: '开始时间不能大于结束时间',
          icon: "none"
        })
      } else {
        this.setData({
          beginTime: util.formatTime2(new Date(event.detail)).split(" ")[0],
        })
        this.onClose()
        this.initData()
      }
    } else if (type == 2) {
      if (new Date(begin).getTime() >= event.detail) {
        wx.showToast({
          title: '开始时间不能大于结束时间',
          icon: "none"
        })
      } else {
        this.setData({
          endTime: util.formatTime2(new Date(event.detail)).split(" ")[0],
        })
        this.onClose()
        this.initData()
      }
    }

  },
  onClose() {
    this.setData({
      show: false
    });
  },
  
  pre() {
    this.setData({
      pageNum: this.data.pageNum - 1
    })
    this.initData();
  },

  next() {
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.initData();
  },

  // 前往提现
  withdraw() {
    this.setData({
      showDialog: true
    })
  },

  async initData(type) {
    if (!type) {
      wx.showLoading({
        title: '加载中...',
      })
    }

    const userResult = await authInfo();
    if (userResult.code == 200) {
      this.setData({
        userInfo: userResult.data.user,
        curMoney: util.fenToYuan(userResult.data.user.curMoney),
        allMoney: util.fenToYuan(userResult.data.user.allMoney)
      })
      const uu = userResult.data.user;
      wx.setStorageSync('userInfo', JSON.stringify({
        id: uu.id,
        openId: uu.openId,
        nickName: uu.nickName,
        avatarUrl: uu.avatarUrl,
        gender: uu.gender,
        identity: uu.identity,
        disabled: uu.disabled,
        allMoney: uu.allMoney,
        curMoney: uu.curMoney,
        createTime: uu.createTime,
        token: app.globalData.token     
      }));
    } else {
      console.log(userResult.message);
    }

    let param = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      beginTime: this.data.beginTime.split("-").join("/") + " 00:00:00",
      endTime: this.data.endTime.split("-").join("/") + " 23:59:59"
    }
    const res = await moneyRecord(param);
    if (res.code == 200 && res.data.count != 0) {
      this.setData({
        list: res.data.content.map(e => {
          return {
            amount: util.fenToYuan(e.amount),
            createTime: e.createTime,
            type: e.type
          }
        }),
        count: res.data.count,
        pages: res.data.page
      })
    } else {
      console.log(res.message);
    }

    wx.hideLoading({
      success: (res) => { },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData();
  },

})